﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AppServidor
{
    class Lista
    {

        string nombre;

        public string Nombre
        {
            get { return nombre; }
            set { nombre = value; }
        }

        public int SizePista
        {
            get {
                if (this.pistas != null)
                    return this.pistas.Count;
                else return 0;
            }
         
        }
        bool random;

        public bool Random
        {
            get { return random; }
            set { random = value; }
        }


        public void setRandom(String valor) {
            random = valor.CompareTo("true")==0;
        }
        bool circular;
        public void setCircular(String valor)
        {
            circular = valor.CompareTo("true") == 0;
        }
        public bool Circular
        {
            get { return circular; }
            set { circular = value; }
        }
        List<string> pistas;

        public List<string> Pistas
        {
            get { return pistas; }
            //set { pistas = value; }
        }

        public Lista() {
            pistas = new List<string>();
        }

        public static Lista abrir_archivo(String name)
        {
            string path = System.IO.Path.Combine(@"C:\FistProjectOLC2\Lists", name);
            FileInfo f = new FileInfo(path);

            if (f.Exists)
            {
                try
                {
                    Lista p = new Lista();
                    
                    p.nombre = name.Replace(".bin","");
                    FileStream fs = new FileStream(path, FileMode.Open);

                    BinaryReader br = new BinaryReader(fs, ASCIIEncoding.UTF8);
                    int r = br.ReadInt32();
                    int c = br.ReadInt32();
                    p.random = (r == 1);
                    p.circular = (c == 1);
                    while (br.PeekChar() != -1)
                    {

                        string pista = br.ReadString();
                        p.add(pista);
                    }

                    br.Close();
                    return p;
                }
                catch (System.Exception)
                {
                    return null;
                }

            }


            return null;
        }
        public static Lista abrir(String nombre)
        {
            string path = System.IO.Path.Combine(@"C:\FistProjectOLC2\Lists", nombre + ".bin");
            FileInfo f = new FileInfo(path);

            if (f.Exists)
            {
                try
                {
                    Lista p = new Lista();
                    p.nombre = nombre;
                    FileStream fs = new FileStream(path, FileMode.Open);

                    BinaryReader br = new BinaryReader(fs, ASCIIEncoding.UTF8);
                    int r= br.ReadInt32();
                    int c = br.ReadInt32();
                    p.random = (r == 1);
                    p.circular = (c == 1);
                    while (br.PeekChar() != -1)
                    {

                     string pista=   br.ReadString();
                     p.add(pista);
                    }
               
                    br.Close();
                    return p;
                }
                catch (System.Exception)
                {
                    return null;
                }

            }


            return null;
        }
        public bool guardar() {
            System.IO.Directory.CreateDirectory(@"C:\FistProjectOLC2");
            System.IO.Directory.CreateDirectory(@"C:\FistProjectOLC2\Lists");
            string path = System.IO.Path.Combine(@"C:\FistProjectOLC2\Lists", nombre + ".bin");
            FileInfo f = new FileInfo(path);
            if (f.Exists)
            {
                string messageBoxText = "Ya existe una lista con este nombre desea actualizarla?";
                string caption = "Lista ya existe";
                MessageBoxButtons button = MessageBoxButtons.YesNo;
                MessageBoxIcon icon = MessageBoxIcon.Warning;
                DialogResult result = MessageBox.Show(messageBoxText, caption, button, icon);
                if (result == DialogResult.No)
                {
                    return false;
                }


            }
            try
            {
                FileStream fs = new FileStream(path, FileMode.Create);

                BinaryWriter bw = new BinaryWriter(fs, ASCIIEncoding.UTF8);
                int r=(random)?1:0;
                int c = (circular) ? 1 : 0;
                bw.Write(r);
                bw.Write(c);
                foreach (string name in this.pistas) {
                    bw.Write(name);
                   
                
                }
                bw.Close();
                return true;
            }
            catch (System.Exception)
            {
                return false;
            }
        }
        int contador_random = 0;
        public void refresh() {
            contador_random = 0;
        }

        public Pista siguientePista(int index) {
           
                if (index < pistas.Count) {
                    return Pista.abrir(pistas[index]);
                }
            

            return null;
        }

        public string generarCodigo() { 
            string lista="";
            String name;
            int tamanio=this.pistas.Count-1;
            for (int i = 0; i <tamanio ;i++ )
            {name=pistas[i];
            lista = lista+name + ",";
            }
            name = pistas[tamanio];
            lista = lista + name;
            string randomtxt = random ? "true" : "false";
            string circulartxt = circular ? "true" : "false";
            
        String script="{lista:\n\t"+  
                    "{nombre: \"Lista ejemplo\",\n\t" +
                    "random: "+randomtxt+",\n\t"+
                    "circular: " + circulartxt + ",\n\t" +
                    "pistas: ["+lista+"]\n\t"+
                     "}\n}";
        return script;
        
        }

        public string generarXmlCodigo()
        {
            string lista = "";
           
            foreach (string name in pistas){
                Pista tmp = Pista.abrir(name);
                if (tmp != null)
                {
                    lista += "< pista nombre = \"" + name + "\" duracion =" + tmp.Duracionms + " />\n";
                }
            }
            string randomtxt = random ? "\"si\"" : "\"no\"";
            string circulartxt = circular ? "\"si\"" : "\"no\"";

            String script = "<lista nombre=\"" +this.nombre+"\" aleatoria="+randomtxt+" circular="+ circulartxt+">\n"+
                            lista+
                            "</lista>";
            return script;

        }

        public override string ToString()
        {
            return nombre;
        }
        public void add(String pista) {

            pistas.Add(pista);
        }
    }
}
